import os
import fitz
import glob
import requests


os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

#剪切pdf为图片
def CutPdf(pdfPath,savePath,pdfSavePath):
	try:
		doc = fitz.open(pdfPath) #pdf路径
		for pg in range(doc.pageCount):
			page = doc[pg]
			rotate = int(0)
		    # 每个尺寸的缩放系数为2，这将为我们生成分辨率提高四倍的图像。
			zoom_x = 2.0
			zoom_y = 2.0
			trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
			pm = page.getPixmap(matrix=trans, alpha=False)
			imagePath = savePath + '%s.png' % str(pg + 1)
			pm.writePNG(imagePath) #最终存储路径
			newpdfPath = pdfSavePath + '%s.pdf' % str(pg + 1)
			print("剪切一张图片：" + str(pg + 1))
			frompic2pdf(imagePath,pdfSavePath,newpdfPath)
			print("转换成功一张pdf：" + str(pg + 1))
	except Exception as e:
		print('CutPdf出现异常:' + str(e))

#图片转pdf
def frompic2pdf(img_path,pdfSavePath, pdf_path):
	# 使用glob读图
	for img in sorted(glob.glob(img_path)):
		if not os.path.exists(pdfSavePath):
			 os.makedirs(pdfSavePath)
		
		# 打开空文档
		doc = fitz.open()
		# 打开指定图片
		imgdoc = fitz.open(img)
		# 使用图片创建单页的PDF
		pdfbytes = imgdoc.convertToPDF()
		imgpdf = fitz.open("pdf", pdfbytes)
		# 将当前页写入文档
		doc.insertPDF(imgpdf)
		# 保存为指定名称的PDF文件
		doc.save(pdf_path)
		# 关闭
		doc.close()


def main():
    pdfUrl = "cards"
    fileList = os.listdir(pdfUrl)
    index = 1
    for item in  fileList:
        print("开始裁剪" + item)
        url = pdfUrl + "/" + item

        foldStr = "images/" + item.split('.')[0]
        url2 = foldStr + "/" + item.split('.')[0]
        foldStr3 = "pdf/" + item.split('.')[0]
        url3 = foldStr3 + "/"
        if not os.path.exists(foldStr):
            os.makedirs(foldStr)

        CutPdf(url,url2,url3)
        print("完成裁剪")
        index+=1

if __name__ == "__main__":
    main()